package com.imau.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imau.bean.User;
import com.imau.common.dataSourceTool.SqlSessionFactoryUtil;
import com.imau.common.entiry.PageQuery;
import com.imau.common.utils.StringUtils;
import com.imau.mapper.UserMapper;
import com.imau.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {
    //操作数据库的session
    private SqlSession session;

    public UserServiceImp() {
        //父类 ServiceImpl属性，自动注入
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        this.baseMapper = session.getMapper(UserMapper.class);
    }

    @Override
    public User queryById(Serializable id) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);

        return this.baseMapper.selectById(id); //super.getById(id);
    }

    @Override
    public List<User> queryList(User entity) {
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(entity);
        //2.调用方法
        return super.list(lqw);
    }

    @Override
    public Page<User> queryPageList(User entity, PageQuery pageQuery) {
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        //1.构建查询条件
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(entity);


        return super.page(pageQuery.build(), lqw);
    }

    @Override
    public boolean updateByEntity(User entity) {
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return this.updateById(entity);
    }

    @Override
    public boolean saveEntity(User entity) {
        return false;
    }

    @Override
    public boolean saveEntity(User entity, List<Serializable> roleIds) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);

        int row = this.baseMapper.insert(entity);

        boolean b = this.assigningRoles(entity.getId(), roleIds);


        return row > 0 && b;
    }

    @Override
    public boolean removeWithById(Serializable id) {

        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        int row = this.baseMapper.deleteRole(id);
        boolean b = this.removeById(id);
        return row >= 0 & b;
    }

    @Transactional(readOnly = true)
    @Override
    public boolean removeWithValidByIds(Collection<?> ids) {
        session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(true);
        return this.removeBatchByIds(ids);
    }

    @Override
    public LambdaQueryWrapper<User> buildQueryWrapper(User bo) {
        //根据字段类别
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDept() != null, User::getDept, bo.getDept().getDid());
        lqw.eq(bo.getEnableState() != null, User::getEnableState, bo.getEnableState());
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), User::getUsername, bo.getUsername());
        lqw.like(StringUtils.isNotBlank(bo.getMobile()), User::getMobile, bo.getMobile());
        lqw.orderByAsc(User::getId);
        return lqw;
    }

    @Override
    public boolean checkLogin(User user) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        return this.baseMapper.selectByMap(map).get(0) != null ? true : false;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean assigningRoles(String uid, List<Serializable> rds) {

        int row1;
        int row2 = 0;

        //1.先删除用户所有角色；中间表用户角色
        row1 = this.baseMapper.deleteRole(uid);

        if (CollectionUtils.isNotEmpty(rds)) {
            //2.添加进去
            for (Serializable rid : rds) {
                row2 = this.baseMapper.addRole(uid, rid);
            }

        }

        return row1 >= 0 && row2 > 0;
    }
}
